package com.ruoyi.food.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.food.api.model.FoodDiscountVo;
import com.ruoyi.food.domain.Food;
import com.ruoyi.food.domain.FoodType;
import com.ruoyi.food.mapper.FoodMapper;
import com.ruoyi.food.service.FoodService;
import com.ruoyi.food.service.FoodTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 食品服务impl
 *
 * @author Administrator
 * @date 2023/03/14
 */
@Service
@RequiredArgsConstructor
public class FoodServiceImpl extends ServiceImpl<FoodMapper, Food> implements FoodService {

    private final FoodTypeService foodTypeService;

    @Override
    public List<FoodDiscountVo> listFoodDetailByFoodIds(List<Long> foodIdList) {
        return baseMapper.listFoodDetailByFoodIds(new QueryWrapper<>().inSql(CollUtil.isNotEmpty(foodIdList), "tf.id", StrUtil.join(",", foodIdList)));
    }

    @Override
    public Map<String, List<Food>> listFood() {
        // 列出所有启用菜品类型
        List<FoodType> list = foodTypeService.list(Wrappers.lambdaQuery(FoodType.class).eq(FoodType::getStatus, 1));
        //TODO 折扣方案目前只设置在了菜品类型上面，没有设置在菜品上面
        if (CollUtil.isNotEmpty(list)) {
            List<Long> ids = list.stream().map(FoodType::getId).collect(Collectors.toList());
            List<Food> foods = baseMapper.selectList(Wrappers.lambdaQuery(Food.class).eq(Food::getStatus, 1).in(Food::getTypeId, ids));
            //TODO 这里应该加上菜品是否允许折扣的条件
            Map<Long, List<Food>> collect = foods.stream().collect(Collectors.groupingBy(Food::getTypeId));
            return list.stream().collect(Collectors.toMap(FoodType::getClassificationName, foodType -> collect.get(foodType.getId())));
        } else {
            return new HashMap<>();
        }
    }
}
